﻿systems = systems || {};
//系统-角色管理
systems.role = function () {
    return {
        setUpAuthority: function () {
            ///	<summary>
            ///	设置权限
            ///	</summary>
            var ids = commons.checkbox.all("grid");
            if (!ids) {
                $.easyui.warn("请选择角色");
                return;
            }
            //弹出操作
            var dialog = function (data) {
                $.easyui.dialog({
                    title: "用户权限设置",
                    buttons: "dialogButtons",
                    url: "/systems/role/UserPermission?ids=" + data,
                    width: 1200,
                    height: 500,
                    icon: "icon-user"
                });
            }
            dialog(ids);
        },

        rolePermissionOperation: function (url, role, type, nowRoleId, applicationId, isCanNull) {
            ///	<summary>
            ///	角色权限操作
            ///	</summary>

            //获取权限编号
            var getResourceIds = function (permissionType) {
                var ids = [];
                var checkedRows = $("input[name=resources_checkbox_" + permissionType + "]");
                $.each(checkedRows,
                    function (i, row) {
                        var $this = $(row);
                        if ($this.is(":checked"))
                            ids.push($this.data("resources_id_" + permissionType));
                    });
                return ids;
            }
            var resourceIds = getResourceIds(type);
            if (isCanNull === false) {
                if (!resourceIds || resourceIds.length === 0) {
                    $.easyui.warn("请选择资源");
                    return;
                }
            }
            $.easyui.ajax(url,
                { type: type, role: role, resourceIds: resourceIds, applicationId: applicationId },
                function (result) {
                    $.easyui.showFormMessage(result);
                    if (result.Code !== $.easyui.state.ok)
                        return;
                    $.easyui.refreshPanel(
                        "panelModule_" + type,
                        "/systems/module/dialogmodulepanel?roleId=" + nowRoleId + "&type=" + type + "&applicationId=" + applicationId);
                },
                "json",
                "POST");
        },
        batchRolePermissionAdd: function (type, nowRoleId, applicationId) {
            ///	<summary>
            ///	批量角色权限添加
            ///	</summary>
            var roleIds = $("#dialogs_role_ids").val();
            systems.role.rolePermissionOperation("/systems/role/batchrolepermissionadd", roleIds.split(","), type, nowRoleId, applicationId, false);
        },
        batchRolePermissionDel: function (type, nowRoleId, applicationId) {
            ///	<summary>
            ///	批量角色权限删除
            ///	</summary>
            var roleIds = $("#dialogs_role_ids").val();
            systems.role.rolePermissionOperation("/systems/role/batchrolepermissiondel", roleIds.split(","), type, nowRoleId, applicationId, false);
        },
        rolePermissionSave: function (roleId, type, applicationId) {
            ///	<summary>
            ///	角色权限保存
            ///	</summary>
            systems.role.rolePermissionOperation("/systems/role/rolepermissionsave", roleId, type, roleId, applicationId, true);
        },
        dialogCheckboxChoice: function (checkboxObj, type) {
            ///	<summary>
            ///	弹出层复选框选择
            ///	</summary>
            var $this = $(checkboxObj);
            var parentPath = $this.attr("data-resources_path_" + type);
            var selected = $this.is(":checked") ? true : false;
            //向下处理
            $("input[data-resources_path_" + type + "^='" + parentPath + "']").prop("checked", selected);
            if (selected === false) {
                return;
            }
            //向上处理
            var parentids = parentPath.split(",");
            for (var i = 0; i < parentids.length; i++) {
                var parentid = parentids[i];
                if (!parentid)
                    continue;
                var $parent = $("input[data-resources_id_" + type + "=" + parentid + "]");
                $parent.prop("checked", true);
            }
        },
        allCheckboxOperation: function (type, state) {
            ///	<summary>
            ///	弹出层所有复选框操作
            ///	</summary>
            var checkbox = $("input[name=resources_checkbox_" + type + "]");
            if (!checkbox)
                return;
            checkbox.prop("checked", state);
        },
        allRoleUserCheckboxOperation: function (state) {
            ///	<summary>
            ///	弹出层所有角色用户复选框操作
            ///	</summary>
            var checkbox = $("input[name=dialog_user_checkbox]");
            if (!checkbox)
                return;
            checkbox.prop("checked", state);
        },
        roleUserOperation: function (url, role, tenantId, nowRoleId, isCanNull) {
            ///	<summary>
            ///	角色用户操作
            ///	</summary>

            //获取用户编号
            var getUserIds = function () {
                var ids = [];
                var checkedRows = $("input[name=dialog_user_checkbox]");
                $.each(checkedRows,
                    function (i, row) {
                        var $this = $(row);
                        if ($this.is(":checked"))
                            ids.push($this.data("dialog_user_id"));
                    });
                return ids;
            }
            var userIds = getUserIds();
            if (isCanNull === false) {
                if (!userIds || userIds.length === 0) {
                    $.easyui.warn("请选择用户");
                    return;
                }
            }
            $.easyui.ajax(url,
                { role: role, userIds: userIds, tenantId: tenantId },
                function (result) {
                    $.easyui.showFormMessage(result);
                    if (result.Code !== $.easyui.state.ok)
                        return;
                    $.easyui.refreshPanel(
                      "panelRoleUser",
                      "/systems/user/dialogindex?roleId=" + nowRoleId + "&tenantId=" + tenantId);
                },
                "json",
                "POST");
        },
        batchRoleUserAdd: function (nowRoleId, tenantId) {
            ///	<summary>
            ///	批量角色用户添加
            ///	</summary>
            var roleIds = $("#dialogs_role_ids").val();
            systems.role.roleUserOperation("/systems/role/batchroleuseradd", roleIds.split(","), tenantId, nowRoleId, false);
        },
        batchRoleUserDel: function (nowRoleId, tenantId) {
            ///	<summary>
            ///	批量角色用户删除
            ///	</summary>
            var roleIds = $("#dialogs_role_ids").val();
            systems.role.roleUserOperation("/systems/role/batchroleuserdel", roleIds.split(","), tenantId, nowRoleId, false);
        },
        roleUserSave: function (roleId, tenantId) {
            ///	<summary>
            ///	角色用户保存
            ///	</summary>
            systems.role.roleUserOperation("/systems/role/roleusersave", roleId, tenantId, roleId, true);
        }
    }
}();